package com.xinghou.platform.dao.main.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.xinghou.platform.dao.bases.JdbcTemplate;
import com.xinghou.platform.dao.bases.database.CustomerContextHolder;
import com.xinghou.platform.dao.main.NoticeDAO;
import com.xinghou.platform.model.main.Notice;

/**
 * 
 * @author wzk
 *
 */
@Repository
public class NoticeDAOImpl  {/*
	@Resource
	private JdbcTemplate jdbcTemplate;
	
	@Override
	public Notice getNoticeById(Integer Id) throws DataAccessException {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_READ);
		String sql = "SELECT Notice_id,Sender_id,Receiver_id,Title,TEXT,Attach_id,Jump_url,Jump_native_page,STATUS,Create_time,type FROM notice WHERE Notice_id = ?";
		Notice attachment = jdbcTemplate.queryForObject(sql, new Object[] { Id }, new NoticeMapper());
		CustomerContextHolder.clearCustomerType();
		return attachment;
	}
	
	public Notice getNoticeByTitle(String title,Integer userId) throws DataAccessException {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_READ);
		String sql = "SELECT Notice_id,Sender_id,Receiver_id,Title,TEXT,Attach_id,Jump_url,Jump_native_page,STATUS,Create_time,type FROM notice WHERE Title = ? AND Receiver_id=?";
		Notice attachment = jdbcTemplate.queryForObject(sql, new Object[] { title,userId }, new NoticeMapper());
		CustomerContextHolder.clearCustomerType();
		return attachment;
	}

	@Override
	public int updateNotice(Notice notice) throws DataAccessException {
		//
		StringBuffer strSQL = new StringBuffer(
				"UPDATE notice SET Sender_id =?,Receiver_id=?,Title=?,TEXT=?,Attach_id=?,Jump_url=?,Jump_native_page =?,STATUS=?,Create_time=?,type=? WHERE Notice_id=?"	
				);
		Object[] params = {notice.getSenderId(),notice.getReceiverId(),notice.getTitle(),notice.getText(),notice.getAttachId(),
				notice.getJumpUrl(),notice.getJumpNativePage(),notice.getStatus(),notice.getCreateTime(),notice.getType(),notice.getNoticeId()
		};
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_WRITE);
		int ret = jdbcTemplate.update(strSQL.toString(),params);
		CustomerContextHolder.clearCustomerType();
		return ret;
	}

	@Override
	public int saveNotice(Notice notice) throws DataAccessException {
		StringBuffer strSQL = new StringBuffer(
				"INSERT INTO notice (Sender_id, Receiver_id, Title, TEXT, Attach_id, Jump_url, Jump_native_page, STATUS, Create_time, type) VALUES (?,?,?,?,?,?,?,?,?,?);"
				);
		Object[] params = {notice.getSenderId(),notice.getReceiverId(),notice.getTitle(),notice.getText(),notice.getAttachId(),
				notice.getJumpUrl(),notice.getJumpNativePage(),notice.getStatus(),notice.getCreateTime(),notice.getType()
		};
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_WRITE);
		int ret =jdbcTemplate.save(strSQL.toString(),params);
		CustomerContextHolder.clearCustomerType();
		return ret;
	}
	
	@Override
	public List<Notice> getNoticeList(Integer sendid, Integer receiverid,
			Integer state, int startNum, int limitNum)
			throws DataAccessException {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_READ);
		int size = 3;
		String sql = "SELECT Notice_id,Sender_id,Receiver_id,Title,TEXT,Attach_id,Jump_url,Jump_native_page,STATUS,Create_time,TYPE FROM notice WHERE "
				+"Receiver_id=?";
		if(state != null && state >=0)
		{
			sql = sql +" AND Status=?";
			size++;
			sql = sql +" AND Create_time < NOW() ORDER BY Notice_id ASC LIMIT ?,?";
		}else{
			//sql = sql +" AND Status=0";
			sql = sql +" AND Create_time < NOW() ORDER BY Notice_id DESC LIMIT ?,?";
		}
		Object[] obj = new Object[size];
		obj[0] = receiverid;
		if(state != null && state >=0)
		{
			obj[1] = state;
			obj[2] = startNum;
			obj[3] = limitNum;
		}else{
			obj[1] = startNum;
			obj[2] = limitNum;
		}
//		int size=2;
//		if(sendid != -1)
//		{
//			size++;
//		}
//		if(state != -1)
//		{
//			size++;
//		}
//		size = size+2;
//		Object[] obj = new Object[size];
//		obj[0] = receiverid; 
//		StringBuffer sql = new StringBuffer();
//		sql.append("SELECT Notice_id,Sender_id,Receiver_id,Title,TEXT,Attach_id,Jump_url,Jump_native_page,STATUS,Create_time,type FROM notice WHERE"
//				+ " Receiver_id = ? AND (TYPE = 2 AND DATE(Create_time) = CURDATE() AND Title != '8') OR (Title = '8' AND DATE(Create_time) = CURDATE() AND TYPE != 2) OR (Title != '8' AND TYPE != 2");
//		if(sendid != -1)
//		{
//			sql.append(" AND Sender_id=?");
//			obj[1] = sendid;
//		}
//		if(state != -1)
//		{
//			sql.append(" AND Status=?");
//			if(sendid != null){
//				obj[2] = state;
//			}else{
//				obj[1] = state;
//			}
//		}
//		sql.append(") AND Create_time<? ORDER BY Create_time DESC limit ?,?");
//		if(sendid !=-1 && state != -1){
//			obj[3] = new Date();
//			obj[4] = startNum;
//			obj[5] = limitNum;
//		}else if((sendid !=-1 && state == -1) || (sendid ==-1 && state != -1)){
//			obj[2] = new Date();
//			obj[3] = startNum;
//			obj[4] = limitNum;
//		}else{
//			obj[1] = new Date();
//			obj[2] = startNum;
//			obj[3] = limitNum;
//		}
		List<Notice> list = jdbcTemplate.query(sql.toString(),obj, new NoticeMapper());
		CustomerContextHolder.clearCustomerType();
		return list;
	}

	@Override
	public int queryMessageCount(Integer sendid, Integer receiverid, int state)
			throws DataAccessException {
		CustomerContextHolder.setCustomerType(CustomerContextHolder.DATA_SOURCE_READ);
		int size=1;
		if(sendid != null)
		{
			size++;
		}
		if(state != -1)
		{
			size++;
		}
		Object[] obj = new Object[size];
		obj[0] = receiverid; 
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT COUNT(Notice_id) FROM notice WHERE Receiver_id = ?");
		if(sendid != null)
		{
			sql.append(" AND Sender_id=?");
			obj[1] = sendid;
		}
		if(state != -1)
		{
			sql.append(" AND Status=?");
			if(sendid != null)
			{
				obj[2] = state;
			}else{
				obj[1] = state;
			}
		}
		int count = jdbcTemplate.queryForObject(sql.toString(),obj, new NoticeCountMapper());
		CustomerContextHolder.clearCustomerType();
		return count;
	}
	
	private static final class NoticeMapper implements RowMapper<Notice> {
		public Notice mapRow(ResultSet rs, int rowNum) throws SQLException {
			Notice notice = new Notice();
			notice.setNoticeId(rs.getInt("Notice_id"));
			notice.setSenderId(rs.getInt("Sender_id"));
			notice.setReceiverId(rs.getInt("Receiver_id"));
			notice.setTitle(rs.getString("Title"));
			notice.setText(rs.getString("Text"));
			notice.setJumpUrl(rs.getString("Jump_url"));
			notice.setJumpNativePage(rs.getString("Jump_native_page"));
			notice.setAttachId(rs.getInt("Attach_id"));
			notice.setStatus(rs.getShort("Status"));
			notice.setCreateTime(rs.getTimestamp("Create_time"));
			notice.setType(rs.getInt("type"));
			return notice;
		}
	}

	private static final class NoticeCountMapper implements RowMapper<Integer> {
		public Integer mapRow(ResultSet rs, int rowNum) throws SQLException {
			Integer count = rs.getInt("count");
			return count;
		}
	}*/
}
